package com.nanhai.buddhist.college.module.study.controller.admin.timetable;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.CollegeClassApi;
import com.nanhai.buddhist.college.module.collegebase.api.room.RoomApi;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.TeacherApi;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableCreateReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableDeleteReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableExcelVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableExportReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetablePageReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableUpdateReqVO;
import com.nanhai.buddhist.college.module.study.convert.timetable.ClassTimetableConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.timetable.ClassTimetableDO;
import com.nanhai.buddhist.college.module.study.service.studysubject.StudySubjectService;
import com.nanhai.buddhist.college.module.study.service.timetable.ClassTimetableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 课程")
@RestController
@RequestMapping("/study/class-timetable")
@Validated
public class ClassTimetableController {

    @Resource
    private ClassTimetableService classTimetableService;


    @PostMapping("/create")
    @ApiOperation("创建课程")
    public CommonResult<Long> createClassTimetable(@Valid @RequestBody ClassTimetableCreateReqVO createReqVO) {
        return success(classTimetableService.createClassTimetable(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新课程")
    public CommonResult<Boolean> updateClassTimetable(@Valid @RequestBody ClassTimetableUpdateReqVO updateReqVO) {
        classTimetableService.updateClassTimetable(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除课程")
    public CommonResult<Boolean> deleteClassTimetable(@Valid @RequestBody ClassTimetableDeleteReqVO deleteReqVO) {
        classTimetableService.deleteClassTimetable(deleteReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得课程")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<ClassTimetableRespVO> getClassTimetable(@RequestParam("id") Long id) {
        ClassTimetableDO classTimetable = classTimetableService.getClassTimetable(id);
        return success(ClassTimetableConvert.INSTANCE.convert(classTimetable));
    }


    @GetMapping("/initClassTimeTableByClassId")
    @ApiOperation("初始课程表")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<Boolean> initClassTimeTableByClassId(String id) {
        classTimetableService.initClassTimeTableByClassId(id);
        return success(true);
    }

    @GetMapping("/list")
    @ApiOperation("获得课程列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<ClassTimetableRespVO>> getClassTimetableList(@RequestParam("ids") Collection<Long> ids) {
        List<ClassTimetableDO> list = classTimetableService.getClassTimetableList(ids);
        return success(ClassTimetableConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getQrCodeStr")
    @ApiOperation("获取生成二维码字符串")
    public CommonResult<String> getQrCodeStr(Long id) {
        return success(classTimetableService.getQrCodeStr(id));
    }


    @GetMapping("/getClassTimetableListByClassId")
    @ApiOperation("根据班级ID获得课程列表")
    @ApiImplicitParam(name = "classId", value = "编号列表", required = true, dataTypeClass = String.class)
    public CommonResult<List<ClassTimetableRespVO>> getClassTimetableListByClassId(@RequestParam("classId") String classId) {
        List<ClassTimetableDO> list = classTimetableService.getClassTimetableListByClassId(classId);
        return success(classTimetableService.coverNames(list));
    }

    @GetMapping("/getClassTimetableListByTeacherId")
    @ApiOperation("根据教师ID获得课程列表")
    @ApiImplicitParam(name = "teacherId", value = "编号列表", required = true, dataTypeClass = String.class)
    public CommonResult<List<ClassTimetableRespVO>> getClassTimetableListByTeacherId(@RequestParam("teacherId") String teacherId) {
        List<ClassTimetableDO> list = classTimetableService.getClassTimetableListByTeacherId(teacherId);
        return success(classTimetableService.coverNames(list));
    }


    @GetMapping("/page")
    @ApiOperation("获得课程分页")
    public CommonResult<PageResult<ClassTimetableRespVO>> getClassTimetablePage(@Valid ClassTimetablePageReqVO pageVO) {
        PageResult<ClassTimetableDO> pageResult = classTimetableService.getClassTimetablePage(pageVO);
        return success(ClassTimetableConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出课程 Excel")
    @OperateLog(type = EXPORT)
    public void exportClassTimetableExcel(@Valid ClassTimetableExportReqVO exportReqVO,
                                          HttpServletResponse response) throws IOException {
        List<ClassTimetableDO> list = classTimetableService.getClassTimetableList(exportReqVO);
        // 导出 Excel
        List<ClassTimetableExcelVO> datas = ClassTimetableConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程.xls", "数据", ClassTimetableExcelVO.class, datas);
    }

}
